package ex.recruit.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import modules.recruit.entity.ApplyBase;
import modules.recruit.entity.MatItem;
import modules.recruit.service.MatItemService;

import com.guinea.core.utils.StringUtils;

import ex.recruit.form.MatReviewFormEx;

public class MatItemServiceEx extends MatItemService {
	ApplyBaseServiceEx applyBaseServiceEx;

	public void saveItemFormEx(MatReviewFormEx matReviewFormEx) {
		Long applyBaseId = matReviewFormEx.getApplyBaseId();
		ApplyBase applyBase = getApplyBaseServiceEx().getApplyBase(applyBaseId);
		saveItem(applyBase, applyBaseId, 1, matReviewFormEx.getCollected1(),
				matReviewFormEx.getCollectedDate1());
		saveItem(applyBase, applyBaseId, 2, matReviewFormEx.getCollected2(),
				matReviewFormEx.getCollectedDate2());
		saveItem(applyBase, applyBaseId, 3, matReviewFormEx.getCollected3(),
				matReviewFormEx.getCollectedDate3());
		saveItem(applyBase, applyBaseId, 4, matReviewFormEx.getCollected4(),
				matReviewFormEx.getCollectedDate4());
		saveItem(applyBase, applyBaseId, 5, matReviewFormEx.getCollected5(),
				matReviewFormEx.getCollectedDate5());
		saveItem(applyBase, applyBaseId, 6, matReviewFormEx.getCollected6(),
				matReviewFormEx.getCollectedDate6());
		saveItem(applyBase, applyBaseId, 7, matReviewFormEx.getCollected7(),
				matReviewFormEx.getCollectedDate7());
		saveItem(applyBase, applyBaseId, 8, matReviewFormEx.getCollected8(),
				matReviewFormEx.getCollectedDate8());
		saveItem(applyBase, applyBaseId, 9, matReviewFormEx.getCollected9(),
				matReviewFormEx.getCollectedDate9());
		saveItem(applyBase, applyBaseId, 10, matReviewFormEx.getCollected10(),
				matReviewFormEx.getCollectedDate10());
		saveItem(applyBase, applyBaseId, 11, matReviewFormEx.getCollected11(),
				matReviewFormEx.getCollectedDate11());
		saveItem(applyBase, applyBaseId, 12, matReviewFormEx.getCollected12(),
				matReviewFormEx.getCollectedDate12());
		saveItem(applyBase, applyBaseId, 13, matReviewFormEx.getCollected13(),
				matReviewFormEx.getCollectedDate13());
		saveItem(applyBase, applyBaseId, 14, matReviewFormEx.getCollected14(),
				matReviewFormEx.getCollectedDate14());
		saveItem(applyBase, applyBaseId, 15, matReviewFormEx.getCollected15(),
				matReviewFormEx.getCollectedDate15());
	}

	public void saveItem(ApplyBase applyBase, Long applyBaseId, long serial,
			String collected, String collectedDate) {
		if (applyBase == null || applyBaseId == null
				|| StringUtils.isBlank(collected)
				|| StringUtils.isBlank(collectedDate))
			return;

		Long serialL = new Long(serial);
		MatItem m = getItemByApplyBaseIdAndSerial(applyBaseId, serialL);
		if (m == null) {
			m = new MatItem();
			m.setBase(applyBase);
			m.setSerial(serialL);
		}
		m.setCollected(collected);
		m.setCollectedDate(collectedDate);
		saveOrUpdate(m);
	}

	public MatItem getItemByApplyBaseIdAndSerial(Long applyBaseId, Long serial) {
		List l = getMatItemDao().find(
				"from MatItem where status = '1' and base.id = ? and serial = ?",
				new Object[] { applyBaseId, serial });
		if (l.size() > 0)
			return (MatItem) l.get(0);
		return null;
	}

	public List getItemsByApplyBaseId(Long applyBaseId) {
		return getMatItemDao()
				.find(
						"from MatItem where status = '1' and base.id = ? order by serial",
						new Object[] { applyBaseId });
	}

	public void bindReviewForm(MatReviewFormEx matReviewFormEx) {
		if (matReviewFormEx == null || matReviewFormEx.getApplyBaseId() == null)
			return;
		List l = getItemsByApplyBaseId(matReviewFormEx.getApplyBaseId());
		MatItem item = null;
		item = findItemInList(1, l);
		matReviewFormEx.setCollected1(item.getCollected());
		matReviewFormEx.setCollectedDate1(item.getCollectedDate());
		item = findItemInList(2, l);
		matReviewFormEx.setCollected2(item.getCollected());
		matReviewFormEx.setCollectedDate2(item.getCollectedDate());
		item = findItemInList(3, l);
		matReviewFormEx.setCollected3(item.getCollected());
		matReviewFormEx.setCollectedDate3(item.getCollectedDate());
		item = findItemInList(4, l);
		matReviewFormEx.setCollected4(item.getCollected());
		matReviewFormEx.setCollectedDate4(item.getCollectedDate());
		item = findItemInList(5, l);
		matReviewFormEx.setCollected5(item.getCollected());
		matReviewFormEx.setCollectedDate5(item.getCollectedDate());
		item = findItemInList(6, l);
		matReviewFormEx.setCollected6(item.getCollected());
		matReviewFormEx.setCollectedDate6(item.getCollectedDate());
		item = findItemInList(7, l);
		matReviewFormEx.setCollected7(item.getCollected());
		matReviewFormEx.setCollectedDate7(item.getCollectedDate());
		item = findItemInList(8, l);
		matReviewFormEx.setCollected8(item.getCollected());
		matReviewFormEx.setCollectedDate8(item.getCollectedDate());
		item = findItemInList(9, l);
		matReviewFormEx.setCollected9(item.getCollected());
		matReviewFormEx.setCollectedDate9(item.getCollectedDate());
		item = findItemInList(10, l);
		matReviewFormEx.setCollected10(item.getCollected());
		matReviewFormEx.setCollectedDate10(item.getCollectedDate());
		item = findItemInList(11, l);
		matReviewFormEx.setCollected11(item.getCollected());
		matReviewFormEx.setCollectedDate11(item.getCollectedDate());
		item = findItemInList(12, l);
		matReviewFormEx.setCollected12(item.getCollected());
		matReviewFormEx.setCollectedDate12(item.getCollectedDate());
		item = findItemInList(13, l);
		matReviewFormEx.setCollected13(item.getCollected());
		matReviewFormEx.setCollectedDate13(item.getCollectedDate());
		item = findItemInList(14, l);
		matReviewFormEx.setCollected14(item.getCollected());
		matReviewFormEx.setCollectedDate14(item.getCollectedDate());
		item = findItemInList(15, l);
		matReviewFormEx.setCollected15(item.getCollected());
		matReviewFormEx.setCollectedDate15(item.getCollectedDate());
	}

	private MatItem findItemInList(long serial, List list) {
		MatItem item = null;
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			item = (MatItem) iter.next();
			if (item.getSerial().longValue() == serial)
				return item;
		}
		Calendar cal=Calendar.getInstance();
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		String date=df.format(cal.getTime());		
		item=new MatItem();
		item.setCollectedDate(date);
		return item;
	}

	public ApplyBaseServiceEx getApplyBaseServiceEx() {
		return applyBaseServiceEx;
	}

	public void setApplyBaseServiceEx(ApplyBaseServiceEx applyBaseServiceEx) {
		this.applyBaseServiceEx = applyBaseServiceEx;
	}

}
